package com.can.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.can.domain.ResponseResult;
import com.can.domain.entity.Role;
import com.can.domain.entity.RoleMenu;
import com.can.domain.vo.PageVO;
import com.can.service.RoleMenuService;
import com.can.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 角色 控制层
 *
 * @author cancan
 * @since 2023-05-12 12:14:39
 */
@RestController
@RequestMapping("/system/role")
public class RoleController {

    @Resource
    private RoleService roleService;
    @Resource
    private RoleMenuService roleMenuService;


    @GetMapping("/list")
    public ResponseResult<PageVO> list(Integer pageNum, Integer pageSize, Role role) {
        return ResponseResult.okResult(roleService.page(pageNum, pageSize, role));
    }

    @PutMapping
    public ResponseResult update(@RequestBody @Valid Role role) {
        roleService.update(role);
        return ResponseResult.okResult();
    }

    @PostMapping
    public ResponseResult add(@RequestBody Role role) {
        roleService.add(role);
        return ResponseResult.okResult();
    }

    @GetMapping("/{id}")
    public ResponseResult get(@PathVariable Long id) {
        return ResponseResult.okResult(roleService.getById(id));
    }

    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable Long id) {
        roleService.removeById(id);
        roleMenuService.remove(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId, id));
        return ResponseResult.okResult();
    }

    @GetMapping("/listAllRole")
    public ResponseResult listAllRole() {
        return ResponseResult.okResult(roleService.list());
    }
}

